/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.feedbackdetail.service.impl;

import com.fanrenqiji.common.utils.RegexUtils;
import com.fanrenqiji.entity.FeedbackEntity;
import com.fanrenqiji.entity.TeacherEntity;
import com.fanrenqiji.modules.business.feedback.dao.FeedbackDao;
import com.fanrenqiji.modules.business.teacher.dao.TeacherDao;
import com.fanrenqiji.modules.sys.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.FeedbackDetailEntity;
import com.fanrenqiji.modules.business.feedbackdetail.vo.FeedbackDetailVO;
import com.fanrenqiji.modules.business.feedbackdetail.dao.FeedbackDetailDao;
import com.fanrenqiji.modules.business.feedbackdetail.service.FeedbackDetailService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 问题反馈明细表业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "feedbackDetail")
@Transactional(readOnly = true)
public class FeedbackDetailServiceImpl implements FeedbackDetailService {

    @Autowired
    private FeedbackDetailDao feedbackDetailDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    DozerBeanMapper beanMapper;

    /**
     * 插入
     *
     * @param feedbackDetail 插入的对象
     * @return 插入数据条数
     */
    @Transactional
    public FeedbackDetailVO insert(FeedbackDetailVO feedbackDetail) {
        FeedbackDetailEntity feedbackDetailEntity = new FeedbackDetailEntity();
        beanMapper.map(feedbackDetail, feedbackDetailEntity);
        feedbackDetailDao.save(feedbackDetailEntity);
        beanMapper.map(feedbackDetailEntity, feedbackDetail);
        return feedbackDetail;
    }


    /**
     * 批量插入
     *
     * @param list 数据列表
     * @return 插入数据条数
     */
    @Transactional
    public void insertList(List<FeedbackDetailVO> list) {
        for (FeedbackDetailVO feedbackDetail : list) {
            insert(feedbackDetail);
        }
    }


    /**
     * 修改
     *
     * @param feedbackDetail 修改的数据
     * @return 修改的数据
     */
    @Transactional
    public FeedbackDetailVO update(FeedbackDetailVO feedbackDetail) {
        FeedbackDetailEntity feedbackDetailEntity = null;
        if (StringUtils.isNotEmpty(feedbackDetail.getId())) {
            feedbackDetailEntity = feedbackDetailDao.findOne(Long.parseLong(feedbackDetail.getId()));
        }
        if (feedbackDetailEntity == null) {
            feedbackDetailEntity = new FeedbackDetailEntity();
        }
        beanMapper.map(feedbackDetail, feedbackDetailEntity);
        feedbackDetailDao.save(feedbackDetailEntity);
        beanMapper.map(feedbackDetailEntity, feedbackDetail);
        return feedbackDetail;
    }

    @Transactional
    public FeedbackDetailVO saveDeal(FeedbackDetailVO feedbackDetail) {
        FeedbackDetailEntity feedbackDetailEntity = null;
        if (StringUtils.isNotEmpty(feedbackDetail.getId())) {
            feedbackDetailEntity = feedbackDetailDao.findOne(Long.parseLong(feedbackDetail.getId()));
        }
        if (feedbackDetailEntity == null) {
            feedbackDetailEntity = new FeedbackDetailEntity();
        }
        beanMapper.map(feedbackDetail, feedbackDetailEntity);
        {
       /*     feedbackDetailEntity.setReadFlag(0);
            feedbackDetailEntity.setReplyTime(new Timestamp(new Date().getTime()));
            String phone = UserUtils.getUser().getMobile();
            List<TeacherEntity> teachers = teacherDao.findTeacherByPhone(phone);
            if (teachers!=null && teachers.size()>0){
                feedbackDetailEntity.setFromTeacher(teachers.get(0));
            }else{
                feedbackDetailEntity.setFromTeacher(teacherDao.findOne(1L));
            }
            if (StringUtils.isNotBlank(feedbackDetail.getFeedbackId())){
                FeedbackEntity feedback = feedbackDao.findOne(Long.parseLong(feedbackDetail.getFeedbackId()));
                feedbackDetailEntity.setFeedback(feedback);

                feedback.setStatus(2);
                feedback.setFromTeacher(feedbackDetailEntity.getFromTeacher());
                feedback.setProcessTime(new Timestamp(new Date().getTime()));
                feedback.setResult(RegexUtils.toSummary(feedbackDetail.getContent()));
                feedbackDao.save(feedback);
            }*/
        }
        String phone = UserUtils.getUser().getMobile();
        List<TeacherEntity> teachers = teacherDao.findTeacherByPhone(phone);
        if (teachers!=null && teachers.size()>0){
            feedbackDetailEntity.setReplyTeacher(teachers.get(0));
        }
        if (StringUtils.isNotBlank(feedbackDetail.getFeedbackId())) {
            FeedbackEntity feedback = feedbackDao.findOne(Long.parseLong(feedbackDetail.getFeedbackId()));
            feedbackDetailEntity.setFeedback(feedback);
        }
        feedbackDetailDao.save(feedbackDetailEntity);
        beanMapper.map(feedbackDetailEntity, feedbackDetail);
        return feedbackDetail;
    }


    /**
     * 删除
     *
     * @param id 数据标识
     * @return 删除的数据条数
     */
    @Transactional
    public void delete(String id) {
        feedbackDetailDao.deleteById(Long.parseLong(id));
    }


    /**
     * 查询所有数据
     *
     * @return 数据列表
     */
    public List<FeedbackDetailVO> getAll() {
        List<FeedbackDetailVO> list = Lists.newArrayList();
        List<FeedbackDetailEntity> entities = feedbackDetailDao.findAllList();
        for (FeedbackDetailEntity feedbackDetailEntity : entities) {
            FeedbackDetailVO feedbackDetailVO = new FeedbackDetailVO();
            beanMapper.map(feedbackDetailEntity, feedbackDetailVO);
            list.add(feedbackDetailVO);
        }
        return list;
    }


    /**
     * 通过条件查询数据列表
     *
     * @param feedbackDetail 查询条件
     * @return 数据列表
     */
    public List<FeedbackDetailVO> getList(FeedbackDetailVO feedbackDetail) {
        List<FeedbackDetailVO> list = Lists.newArrayList();
        DetachedCriteria dc = feedbackDetailDao.createDetachedCriteria();
        dc.add(Restrictions.eq(FeedbackDetailEntity.DEL_FLAG, FeedbackDetailEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("feedbackDetail", "feedbackDetail"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(feedbackDetail.getName())) {
        dc.add(Restrictions.like("name", "%" + feedbackDetail.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(feedbackDetail.getName())) {
        dc.add(Restrictions.eq("name", feedbackDetail.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<FeedbackDetailEntity> entities = feedbackDetailDao.find(dc);
        for (FeedbackDetailEntity feedbackDetailEntity : entities) {
            FeedbackDetailVO feedbackDetailVO = new FeedbackDetailVO();
            beanMapper.map(feedbackDetailEntity, feedbackDetailVO);
            list.add(feedbackDetailVO);
        }
        return list;
    }


    /**
     * 通过id查询数据
     *
     * @param id 数据标识
     * @return 数据对象
     */
    public FeedbackDetailVO get(String id) {
        FeedbackDetailVO feedbackDetail = new FeedbackDetailVO();
        FeedbackDetailEntity feedbackDetailEntity = feedbackDetailDao.findOne(Long.parseLong(id));
        if (feedbackDetailEntity == null) {
            feedbackDetailEntity = new FeedbackDetailEntity();
        }
        beanMapper.map(feedbackDetailEntity, feedbackDetail);
        return feedbackDetail;
    }


    /**
     * 根据条件分页查询
     *
     * @param page
     * @param feedbackDetail
     * @return
     */
    public Page<FeedbackDetailVO> findFeedbackDetailByPage(Page<FeedbackDetailVO> page, FeedbackDetailVO feedbackDetail) {
        Page<FeedbackDetailEntity> pageEntity = new Page<FeedbackDetailEntity>(page.getPageNo(), page.getPageSize());
        DetachedCriteria dc = feedbackDetailDao.createDetachedCriteria();
        dc.add(Restrictions.eq(FeedbackDetailEntity.DEL_FLAG, FeedbackDetailEntity.DEL_FLAG_NORMAL));
    /*
        if (StringUtils.isNotEmpty(feedbackDetail.getName())) {
            dc.add(Restrictions.like("name", "%" + feedbackDetail.getName() + "%")); //like
        }

    dc.createAlias("feedbackDetail", "feedbackDetail"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(feedbackDetail.getName())) {
        dc.add(Restrictions.eq("name", feedbackDetail.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
        if (!StringUtils.isNotEmpty(page.getOrderBy())) {
            dc.addOrder(Order.desc("id"));
        }
        pageEntity = feedbackDetailDao.find(pageEntity, dc);
        beanMapper.map(pageEntity, page);
        List<FeedbackDetailVO> list = Lists.newArrayList();
        for (FeedbackDetailEntity feedbackDetailEntity : pageEntity.getList()) {
            FeedbackDetailVO feedbackDetailVO = new FeedbackDetailVO();
            beanMapper.map(feedbackDetailEntity, feedbackDetailVO);
            list.add(feedbackDetailVO);
        }
        page.setList(list);
        return page;
    }

    /**
     * 根据回复查回复详情
     *
     * @param id
     * @return
     */
    public List<FeedbackDetailVO> findFeedbackDetailEntityByFeedbackId(String id) {
        List<FeedbackDetailEntity> list = feedbackDetailDao.findFeedbackDetailEntityByFeedbackId(Long.parseLong(id));
        List<FeedbackDetailVO> list1=new ArrayList<FeedbackDetailVO>();
        for (FeedbackDetailEntity feedbackDetailEntity : list) {
            FeedbackDetailVO feedbackDetailVO = new FeedbackDetailVO();
            beanMapper.map(feedbackDetailEntity,feedbackDetailVO);
            list1.add(feedbackDetailVO);
        }
        return list1;
    }
}
